export default {
	data() {
		return {
			isAddingSpecialRule: false,
			parkSpecialChargeVoList: {
				ruleList: []
			},
			specialRuleItemInitData: {
				isShow: true,
				chargeName: '',
				chargeType: 5,
				enableFlag: true,
				id: null,
				parkID: this.$route.query.parkId,
				parkSpecialChargeFreeVo: {
					parkSpecialChargeDateTimeList: [
						{
							endTime: '',
							startTime: ''
						}
					]
				},
				parkSpecialChargeAllDayTopVo: {
					institutionType: 0,
					parkSpecialChargeAllDayTopNaturalDayVo: null,
					parkSpecialChargeAllDayTopParkingDurationVo: {
						maxValue: ''
					}
				},
				parkSpecialChargeAllDayTotalVo: {
					addUpType: 0,
					addUpValue: 0,
					institutionType: 0
				},
				parkSpecialChargeAmountPrefrentialVo: {
					parkSpecialChargeDateTimeList: [
						{
							endTime: '',
							startTime: ''
						}
					],
					prefrentialType: 0,
					prefrentialValue: 0
				},
				parkSpecialChargeTimePrefrentialVo: {
					parkSpecialChargeDateTimeList: [
						{
							endTime: '',
							startTime: ''
						}
					],
					parkingTimePrefrentialMinutes: 0
				},
				parkSpecialChargeServiceConditionVo: {
					serviceConditionType: '',
					serviceConditionValue: []
				},
				parkSpecialChargeRepeatShareFreeVo: {
					institutionType: 1,
					parkSpecialChargeRepeatShareFreeParkingDurationVo: null,
					parkSpecialChargeRepeatShareFreeNaturalDayVo: {
						startTime: null,
						freeValue: ''
					}
				}
			},
			specialRuleTestRules: {
				chargeName: [{ required: true, message: '请输入规则名称', trigger: 'blur' }],
				'parkSpecialChargeRepeatShareFreeVo.parkSpecialChargeRepeatShareFreeNaturalDayVo.freeValue': [
					{ required: true, message: '请输入免费时长', trigger: 'blur' }
				],
				'parkSpecialChargeServiceConditionVo.serviceConditionType': [
					{ required: true, message: '请选择使用条件', trigger: 'change' }
				],
				'parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopParkingDurationVo.maxValue': [
					{ required: true, message: '请输入封顶金额', trigger: 'blur' }
				]
			},
			specialRuleVipTypes: null,
			curEditSpRuleItem: null
		};
	},
	created() {
		if (this.specialRuleVipTypes === null) {
			this.getSpecialRuleVipTypes();
		}
		this.getParkSpecialChargeVoList();
	},
	methods: {
		getParkSpecialChargeVoList() {
			API.fetchGet('/operatorPark/getParkSpecialChargeVoList', {
				parkID: this.$route.query.parkId
			}).then(
				res => {
					if (res.data) {
						res.data.forEach(item => {
							for (let key in item) {
								if (item[key] === null) {
									item[key] = this.specialRuleItemInitData[key];
								}
							}
							(item.isShow = false), (item.isEdit = false);
							if (
								item.parkSpecialChargeServiceConditionVo.serviceConditionType == 3 ||
								item.parkSpecialChargeServiceConditionVo.serviceConditionType == 2
							) {
								item.parkSpecialChargeServiceConditionVo.serviceConditionValue = item.parkSpecialChargeServiceConditionVo.serviceConditionValue.split(
									','
								);
							}
						});
						this.parkSpecialChargeVoList.ruleList = res.data;
					}
				},
				err => {}
			);
		},
		getSpecialRuleVipTypes() {
			API.fetchPost('/operatorPark/selectVipTypeListBy', {
				parkId: this.$route.query.parkId,
				vipGroupType: 0
			}).then(res => {
				this.specialRuleVipTypes = res.data;
				this.arrToString(this.parkSpecialChargeVoList.ruleList);
			});
		},
		// 特殊收费规则-使用条件-vip类型 (数组转字符串)
		arrToString(ruleList) {
			for (let i = 0; i < ruleList.length; i++) {
				if (ruleList[i].parkSpecialChargeServiceConditionVo.serviceConditionType == 2) {
					let arr = [];
					let tmpArr = ruleList[i].parkSpecialChargeServiceConditionVo.serviceConditionValue;
					if (Array.isArray(tmpArr) && tmpArr.length) {
						for (let j = 0; j < tmpArr.length; j++) {
							for (let k = 0; k < this.specialRuleVipTypes.length; k++) {
								let obj = this.specialRuleVipTypes[k];
								if (obj.id + '' == tmpArr[j]) {
									arr.push(obj.customVipName);
									break;
								}
							}
						}
					}
					this.parkSpecialChargeVoList.ruleList[
						i
					].parkSpecialChargeServiceConditionVo.serviceConditionValue = arr.join(',');
				}
			}
		},
		setSpecialRuleInitData(val) {
			this.specialRuleItemInitData = {
				isShow: true,
				chargeName: '',
				chargeType: val ? val : 5,
				enableFlag: true,
				id: null,
				parkID: this.$route.query.parkId,
				parkSpecialChargeFreeVo: {
					parkSpecialChargeDateTimeList: [
						{
							endTime: '',
							startTime: ''
						}
					]
				},
				parkSpecialChargeAllDayTopVo: {
					institutionType: 0,
					parkSpecialChargeAllDayTopNaturalDayVo: null,
					parkSpecialChargeAllDayTopParkingDurationVo: {
						maxValue: ''
					}
				},
				parkSpecialChargeAllDayTotalVo: {
					addUpType: 0,
					addUpValue: 0,
					institutionType: 0
				},
				parkSpecialChargeAmountPrefrentialVo: {
					parkSpecialChargeDateTimeList: [
						{
							endTime: '',
							startTime: ''
						}
					],
					prefrentialType: 0,
					prefrentialValue: 0
				},
				parkSpecialChargeTimePrefrentialVo: {
					parkSpecialChargeDateTimeList: [
						{
							endTime: '',
							startTime: ''
						}
					],
					parkingTimePrefrentialMinutes: 0
				},
				parkSpecialChargeServiceConditionVo: {
					serviceConditionType: '',
					serviceConditionValue: []
				},
				parkSpecialChargeRepeatShareFreeVo: {
					institutionType: 1,
					parkSpecialChargeRepeatShareFreeParkingDurationVo: null,
					parkSpecialChargeRepeatShareFreeNaturalDayVo: {
						startTime: null,
						freeValue: ''
					}
				}
			};
		},
		addSpecialRule() {
			this.isAddingSpecialRule = true;
			this.setSpecialRuleInitData();
		},
		editSpItem(item) {
			this.curEditSpRuleItem = JSON.parse(JSON.stringify(item));
			if (!item.isEdit || item.isShow === false) {
				item.isShow = true;
				item.isEdit = true;
			} else {
				item.isEdit = false;
			}
		},
		cancelEditSpRule(list, index, item) {
			list.splice(
				index,
				1,
				Object.assign(this.curEditSpRuleItem, {
					isShow: true,
					isEdit: false
				})
			);
		},
		async SaveEditSpItem(list, index, item) {
			let vaild = await this.testSpecialListRule();

			if (vaild) {
				// this.$refs['parkSpecialChargeVoList'].resetFields();
				console.log(JSON.parse(JSON.stringify(item)));
				let saveData = JSON.parse(JSON.stringify(item));
				if (Array.isArray(saveData.parkSpecialChargeServiceConditionVo.serviceConditionValue)) {
					saveData.parkSpecialChargeServiceConditionVo.serviceConditionValue = saveData.parkSpecialChargeServiceConditionVo.serviceConditionValue.join(
						','
					);
				}
				API.fetchPost('/operatorPark/editParkSpecialChargeVo', saveData).then(
					res => {
						if (res.status === 1) {
							this.showToast('修改特殊收费规则成功', 1, 1500);
							item.isEdit = false;
						} else {
							this.showToast(res.message, 2, 1500);
						}
					},
					err => {
						console.log(err);
					}
				);
			}
		},
		serviceConditionTypeChange(val) {
			if (val === 1) {
				this.specialRuleItemInitData.parkSpecialChargeServiceConditionVo.serviceConditionValue = '';
			}
			if (val === 2 || val === 3) {
				this.specialRuleItemInitData.parkSpecialChargeServiceConditionVo.serviceConditionValue = [];
			}
		},
		listItemServiceConditionTypeChange(val, item) {
			if (val === 1) {
				item.parkSpecialChargeServiceConditionVo.serviceConditionValue = '';
			}

			if (val === 3 || val === 2) {
				item.parkSpecialChargeServiceConditionVo.serviceConditionValue = [];
			}
		},
		async addSpecialRuleToList() {
			let vaild = await this.testAddSpecialRule();

			if (vaild) {
				let saveData = JSON.parse(JSON.stringify(this.specialRuleItemInitData));
				if (Array.isArray(saveData.parkSpecialChargeServiceConditionVo.serviceConditionValue)) {
					saveData.parkSpecialChargeServiceConditionVo.serviceConditionValue = saveData.parkSpecialChargeServiceConditionVo.serviceConditionValue.join(
						','
					);
				}
				API.fetchPost('/operatorPark/addParkSpecialChargeVo', saveData).then(
					res => {
						if (res.status === 1) {
							this.showToast('添加特殊收费规则成功', 1, 1500);
							this.specialRuleItemInitData = null;
							this.isAddingSpecialRule = false;
							this.getParkSpecialChargeVoList();
						} else {
							this.showToast(res.message, 2, 1500);
						}
					},
					err => {
						console.log(err);
					}
				);

				// this.parkSpecialChargeVoList.ruleList.push(this.specialRuleItemInitData);
			}
		},
		testAddSpecialRule() {
			return new Promise((resolve, reject) => {
				this.$refs['specialRuleItemInitData'].validate(valid => {
					if (valid) {
						resolve(true);
					} else {
						resolve(false);
					}
				});
			});
		},
		delSpecialRuleItem(list, index, item) {
			if (Array.isArray(item.parkSpecialChargeServiceConditionVo.serviceConditionValue)) {
				item.parkSpecialChargeServiceConditionVo.serviceConditionValue = item.parkSpecialChargeServiceConditionVo.serviceConditionValue.join(
					','
				);
			}
			const saveData = [item];
			API.fetchPost('/operatorPark/delParkSpecialChargeVo', saveData).then(
				res => {
					if (res.status === 1) {
						this.showToast('删除特殊收费规则成功', 1, 1500);
						list.splice(index, 1);
					} else {
						this.showToast(res.message, 2, 1500);
					}
				},
				err => {
					console.log(err);
				}
			);
		}
	},
	watch: {
		'specialRuleItemInitData.chargeType'(val) {
			this.setSpecialRuleInitData(val);
		}
	}
};
